<?php
/**
 *
 * 版权所有：安德玛(underArmour)
 * 作    者：1554324405@qq.com
 * 日    期：2020-05-20
 * 版    本：1.0.0
 * 功能说明：通知公告控制器。
 *
 **/
namespace Api\Controller;

class MessageController extends ComController
{
    /**
     * 消息列表
     */
    public function index()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']) : '10';
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $type = isset($_GET['type']) ? intval($_GET['type']) : '1';
        
        $where['uid'] = array('in',$this->user['id'].',0');
        $notWhere['uid'] = $this->user['id'];
        if($type != 1){
            $notWhere['status'] = 1;
        }
        $notIdArr = M('user_readmessage')->where($notWhere)->field('msg_id')->select();
        $notIds = '';
        if($notIdArr){
            foreach($notIdArr as $key=>$vals){
                $notIds.=$vals['msg_id'].',';
            }
            $notIds = rtrim($notIds, ','); 
        }
        if($type == 1){
            //未读
            $where['id'] = array('not in',$notIds);
        }else if($type == 2){
            //已读
            $where['id'] = array('in',$notIds);
        } 
        $model = M('user_message');
        $count = $model->where($where)->count();
        $list = $model->where($where)->limit($offset . ',' . $pagesize)->order('t desc')->select();

        foreach ($list as $key=>&$val){
            $list[$key]['t'] = date('Y-m-d H:i:s',$list[$key]['t']);
            $list[$key]['content'] = str_replace('<br>', '', $list[$key]['content']);
            if($val['type'] == 1){
                $list[$key]['url'] = C('SITE_URL').$val['url'];
            }
        }
        $page = new \Think\Page($count, $pagesize);
        $page->show();
        $page_arr = array(
            'totalCount'=>intval($count),
            'pageSize'=>$pagesize,
            'totalPage'=>$page->totalPages,
            'currPage'=>intval($p)
        );
        $this->res['data']['page'] = $page_arr;
        $this->res['data']['list'] = $list;
        $this->response($this->res,'json');
    }
    
    /**
     * 消息设为已读
     */
    public function readMessage()
    {
        $id = I('get.id','','htmlspecialchars');
        if(!$id){
            $this->res['code'] = 101;
            $this->res['msg'] = '请传入id';
            $this->response($this->res,'json');
        }
        
        $info = M('user_message')->where('id='.$id)->find();
        if(empty($info)){
            $this->res['code'] = 102;
            $this->res['msg'] = '记录不存在';
            $this->response($this->res,'json');
        }
        $count = M('user_readmessage')->where(array('msg_id'=>$id,'uid'=>$this->user['id']))->count();
        if($count == 0){
            $res = M('user_readmessage')->data(array('msg_id'=>$id,'uid'=>$this->user['id']))->add();
            if($res){
                $this->response($this->res,'json');
            }else{
                $this->res['code'] = 103;
                $this->res['msg'] = 'fail to change the reading';
                $this->response($this->res,'json');
            }
        }else{
            $this->response($this->res,'json');
        } 
    }
    
    /**
     * 删除消息
     */
    public function delete()
    {
        $id = I('get.id','','htmlspecialchars');
        if(!$id){
            $this->res['code'] = 101;
            $this->res['msg'] = '请传入id';
            $this->response($this->res,'json');
        }
        $info = M('user_message')->where('id='.$id)->find();
        if(empty($info)){
            $this->res['code'] = 102;
            $this->res['msg'] = '记录不存在';
            $this->response($this->res,'json');
        }
        
        $count = M('user_readmessage')->where(array('msg_id'=>$id,'uid'=>$this->user['id']))->count();
        if($count == 0){
            M('user_readmessage')->data(array('msg_id'=>$id,'uid'=>$this->user['id'],'status'=>2))->add();
        }else{
            M('user_readmessage')->data(array('status'=>2))->where(array('msg_id'=>$id,'uid'=>$this->user['id']))->save();
        }
        $this->response($this->res,'json');
    }
}